package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketAdMapper;

import com.cskaoyan.module.bo.AdListParamBo;
import com.cskaoyan.module.pojo.MarketAd;
import com.cskaoyan.module.pojo.MarketAdExample;
import com.cskaoyan.module.pojo.MarketCoupon;
import com.cskaoyan.module.pojo.MarketCouponExample;
import com.cskaoyan.module.vo.MarketAdVo;
import com.cskaoyan.module.vo.MarketCouponVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * AdServiceImpl
 *
 * @param
 * @return
 * @2022/4/7
 **/
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper adMapper;

    @Override
    public MarketAdVo list(AdListParamBo params) {
        MarketAdVo result=new MarketAdVo();
//        分页语句
        PageHelper.startPage(params.getPage(),params.getLimit());
        MarketAdExample example=new MarketAdExample();
        example.setOrderByClause(params.getSort()+ " "+params.getOrder());
        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!"".equals(params.getName())) {
            criteria.andNameLike("%"+params.getName()+"%");
        }
        if (!"".equals(params.getContent())) {
            criteria.andContentLike("%"+params.getContent()+"%");
        }
        List<MarketAd> list = adMapper.selectByExample(example);
        int code=0;
        for (MarketAd marketAd : list) {
            marketAd.setUpdateTime(new Date());
            code=adMapper.updateByPrimaryKey(marketAd);
        }
        PageInfo<MarketAd> pageInfo=new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        result.setList(list);
        result.setLimit(pageInfo.getSize());
        //        page? pages?
        result.setPage(pageNum);
        result.setPages(pages);
        result.setTotal((int) total);
        return result;
    }

    @Override
    public MarketAd create(MarketAd params) {
//        insert 并保存最大的id 再通过id查询
        params.setAddTime(new Date());
        params.setUpdateTime(new Date());
        params.setDeleted(false);
        int insertAd = adMapper.insertSelective(params);
        System.out.println("MarketAd after insert maxid = " + params.getId());
        MarketAd marketAd = adMapper.selectByPrimaryKey(params.getId());
        return marketAd;
    }

    @Override
    public MarketAd update(MarketAd params) {
        params.setUpdateTime(new Date());
        int update = adMapper.updateByPrimaryKeySelective(params);
        MarketAd marketAd = adMapper.selectByPrimaryKey(params.getId());
        return marketAd;
    }

    @Override
    public Integer delete(MarketAd params) {
        params.setDeleted(true);
        int update = adMapper.updateByPrimaryKeySelective(params);
        return update;
    }

}
